layui.use(['layer', 'form'], function () {
    var layer = layui.layer
        , $ = layui.$
        , form = layui.form;
    var lineData = getLineData();
    $('.closePage').on('click', function () {
        closeSonPage();
    });
    function closeSonPage() {
        parent.layer.closeAll();
    }
    //下拉框非空验证
    form.verify({
        scriptPath: function (value, item) {
            if (value == '') {
                return "必选项不能为空"
            }
        },
        formTextLength: function (value) {
            var i,sum;
            sum=0;
            for(i=0;i<value.length;i++){
                if ((value.charCodeAt(i)>=0) && (value.charCodeAt(i)<=255)){
                    sum=sum+1;
                } else {
                    sum=sum+2;
                }
            }
            if (sum > 200) {
                return '最多只能200个字符或者100个中文字';
            }
        }
    });
    //监听提交
    form.on('submit(formSubmit)', function (data) {
        var paramData = data.field;
        var data={};
        var url="";
        if (lineData == null) {
            url="/groovy/addService";
            data={ name:paramData.serviceName, path: paramData.scriptPath, method: paramData.scriptMethod
                ,service: paramData.serviceUrl, describe: paramData.desc };
        }else{
            url="/groovy/updateService";
            data={ id:lineData.id,name:paramData.serviceName, path: paramData.scriptPath, method: paramData.scriptMethod
                ,service: paramData.serviceUrl, describe: paramData.desc };
        }
        if(!$(this).attr('lock')||$(this).attr('lock') == "false"){
            $(this).attr('lock',true);
            $.ajax({
                url: url,
                type: 'post',
                data: data,
                contentType: 'application/x-www-form-urlencoded',
                success: function (data) {
                    if (data.code == 200) {
                        layer.msg(data.msg, { time: 900, icon: 6 }, function () {
                            window.parent.renderTable();
                            closeSonPage();
                        });//失败的表情5 成功的表情6
                        return;
                    } else {
                        layer.msg(data.msg, { icon: 5 });//失败的表情5 成功的表情6
                        return;
                    }
                },
            });
            $(this).attr('lock',false);
        }
        return false;
    });
    form.on('select(scriptPathFilter)', function (data) {
        getScriptFunction(data.value,data.value,"");
    });
    function getAllScript() {
        $.ajax({
            url: '/groovy/fileList',
            type: 'get',
            contentType: 'application/x-www-form-urlencoded',
            success: function (data) {
                $("#scriptPath").empty();
                $("#scriptPath").append('<option value="">请选择</option>');
                var opations='';
                for (var index = 0; index < data.data.length; index++) {
                    var element = data.data[index];
                    opations+='<option value="'+element.gpath+'">'+ element.gname +'</option>';
                }
                $("#scriptPath").append(opations);
                renderServicePage();
                layui.form.render("select");
            }
        });
    }
    function renderServicePage() {
        if (lineData!=null){
            $('input[name="serviceName"]').val(lineData.serviceName);
            $('input[name="serviceUrl"]').val(lineData.serviceUrl);
            $('textarea[name="desc"]').val(lineData.desc);
            $('select[name="scriptPath"]').val(lineData.scriptPath);
            getScriptFunction(lineData.scriptPath,lineData.scriptPath,lineData.scriptMethod);
        }
    }
    function getScriptFunction(name,path,method) {
        $.ajax({
            url: '/groovy/getMethod',
            type: 'post',
            data: { name:name, path: path},
            contentType: 'application/x-www-form-urlencoded',
            success: function (data) {
                if (data.code==200){
                    $("#scriptMethod").empty();
                    $("#scriptMethod").append('<option value="">请选择</option>');
                    var opations='';
                    for (var index = 0; index < data.data.length; index++) {
                        var element = data.data[index];
                        opations+='<option value="'+element+'">'+ element +'</option>';
                    }
                    $("#scriptMethod").append(opations);
                    $('select[name="scriptMethod"]').val(method);
                    layui.form.render("select");
                } else{
                    layer.msg(data.msg, { icon: 5 });//失败的表情5 成功的表情6
                    return;
                }

            }
        });
    }
    getAllScript();
});
